#include "play.h"
#include<QIcon>
#include<QPainter>
#include<QPushButton>
#include<QIcon>
#include<QPixmap>
#include"check.h"
#include<QPen>
#include<QFont>
#include<QString>
#include<QMessageBox>
#include<QDebug>
#include<QFile>
#include<QDir>
play::play(QWidget *parent) : QWidget(parent)
{
    direction_current=UP;
    my_snack.up_direction();
    my_snack.up_direction();



    this->setWindowIcon(QIcon(":/res/ico.png"));
    this->setWindowTitle("游戏开始");
    this->setFixedSize(800,600);

    QIcon icon(":res/up.png");
    //icon.actualSize(QSize(100,100));
    QPushButton *up=new QPushButton(this);
   // up->setStyleSheet("QPushButton {width: 50 px  ;height:50 }");
    //这里就会有一个进步的点，如何让我们的图标适应按钮大小
    up->setIcon(icon);
    up->move(680,300);
    connect(up,&QPushButton::clicked,this,[=](){
        direction_current=UP;
    });
//    QPushButton *button = new QPushButton(this);
//    button->setGeometry(680, 300, 50, 50);
//    button->setStyleSheet("border-image:url(:res/up.png)");


    QIcon icon2(":res/down.png");
    QPushButton *down=new QPushButton(this);

    down->setIcon(icon2);
    down->move(680,350);
    connect(down,&QPushButton::clicked,this,[=](){
        direction_current=DOWN;
    });


    icon.addFile(":res/left.png");
    QPushButton *left=new QPushButton(this);
    left->setIcon(icon);
    left->move(640,325);
    connect(left,&QPushButton::clicked,this,[=](){
        direction_current=LEFT;
    });


    icon.addFile(":res/right.png");
    QPushButton *right=new QPushButton(this);
    right->setIcon(icon);
    right->move(720,325);

    connect(right,&QPushButton::clicked,this,[=](){
        direction_current=RIGHT;
    });

    //退出游戏键
   QPushButton * exit_game=new QPushButton(this);
   exit_game->move(655,400);
   QFont my_font("微软雅黑",28);
   exit_game->setText("退出游戏");
   connect(exit_game,&QPushButton::clicked,this,[=](){
        my_timer->stop();
        int res=QMessageBox::warning(this,"游戏结束","是否继续",QMessageBox::Ok|QMessageBox::Cancel);
        if(res==QMessageBox::Ok)
        {
            check * chack_back =new check;
            chack_back->show();
            this->close();
            QString path=QDir::currentPath();
            //qDebug()<<path;
            QFile write_score(path+"/"+"data.txt");

            write_score.open(QIODevice::WriteOnly);
            QString data= QString::number(count);
            write_score.write(data.toUtf8());

        }
        else
        {

            my_timer->start();
        }
   });

    //返回键
    QPushButton *back=new QPushButton(this);
    icon.addFile(":res/back.png");
    back->setIcon(icon);
    back->move(750,525);
    check * chack_back =new check;
    connect(back,&QPushButton::clicked,this,[=](){
        //返回游戏选择界面

        chack_back->show();
        this->close();
    });




    //初始化定时器
    my_timer=new QTimer;
    my_timer->start(chack_back->get_speed());

    connect(my_timer,&QTimer::timeout,this,[=](){


        if(my_snack.list.front().intersects(food_snack))
        {
            creat_food();
            if(direction_current==UP)
                my_snack.up_direction();
            else if(direction_current==DOWN)
                my_snack.down_direction();
            else if(direction_current==LEFT)
                my_snack.left_direction();
            else
                my_snack.right_direction();
            count++;
        }
        if(direction_current==UP)
            my_snack.up_direction();
        else if(direction_current==DOWN)
            my_snack.down_direction();
        else if(direction_current==LEFT)
            my_snack.left_direction();
        else
            my_snack.right_direction();

        my_snack.list.pop_back();

        update();
    });

    //初始化食物
    creat_food();


}

void play::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap back_ground(":/res/game_room.png");
    painter.drawPixmap(0,0,600,600,back_ground);
    back_ground.load(":/res/bg1.png");
    painter.drawPixmap(600,0,200,700,back_ground);


    //分数显示
    back_ground.load(":/res/sorce_bg.png");
    painter.drawPixmap(650,60,100,40,back_ground);

    //写分数
    QPen pen;
    pen.setColor(QColor(Qt::black));
    painter.setPen(pen);
    painter.setFont(QFont("微软雅黑",18));
    painter.drawText(690,95,QString::number(count));

    //渲染小蛇,所以理解矩形起到的作用，定位的作用
    //头
    if(direction_current==UP)
        back_ground.load(":/res/up.png");
    else if(direction_current==DOWN)
        back_ground.load(":/res/down.png");
    else if(direction_current==LEFT)
        back_ground.load(":/res/left.png");
    else
        back_ground.load(":/res/right.png");
    QRect curr_pos=my_snack.list.front();
    painter.drawPixmap(curr_pos.x(),curr_pos.y(),curr_pos.width(),curr_pos.height(),back_ground);

    //身体
    back_ground.load(":/res/Bd.png");
    for(int i=1;i<my_snack.list.size();i++)
    {
        curr_pos=my_snack.list[i];
        painter.drawPixmap(curr_pos.x(),curr_pos.y(),curr_pos.width(),curr_pos.height(),back_ground);
    }

    //食物
    back_ground.load(":/res/food.bmp");
    painter.drawPixmap(food_snack.x(),food_snack.y(),30,30,back_ground);


    if(game_over_check())
    {
        int res=QMessageBox::warning(this,"游戏结束","是否继续",QMessageBox::Ok|QMessageBox::Cancel);
        if(res==QMessageBox::Ok)
        {
            check * chack_back =new check;
            chack_back->show();
            this->close();

        }
        else
        {
            this->close();
        }
        QFile write_score("./data.txt");
        write_score.open(QIODevice::WriteOnly);
        QString data= QString::number(count);
        write_score.write(data.toUtf8());
        //qDebug()<<"游戏结束";
    }

}

void play::creat_food()
{
    food_snack=QRect(qrand()%600,qrand()%600,30,30);
}

bool play::game_over_check()
{
    QRect head=my_snack.list.front();
    for(int i=1;i<my_snack.list.size();i++)
    {
        if(head==my_snack.list[i])
            return true;
    }
    return false;
}
